package com.liqi.sms.utils;

import com.liqi.common.utils.StringUtils;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.springframework.util.Base64Utils;

import java.nio.charset.StandardCharsets;

public class Sm3Util {

    private Sm3Util() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * SM3算法
     *
     * @param plain 加密数据值
     * @return base64摘要的值
     */
    public static String digest(String plain) {

        if (StringUtils.isBlank(plain)) {
            return "";
        }

        SM3Digest sm3 = new SM3Digest();

        byte[] md = new byte[32];

        byte[] psw = plain.getBytes(StandardCharsets.UTF_8);

        sm3.update(psw, 0, psw.length);

        sm3.doFinal(md, 0);

        return Base64Utils.encodeToString(md);

    }

}
